<template>
  <div class="app-container org">
    <el-card style="width: 100%">
      <!-- 搜索条件盒子 -->
      <p class="pinformation">搜索条件</p>
      <el-form :model="queryParams" ref="queryRef" label-width="100px">
        <el-row>
          <el-col :span="4">
            <el-form-item label="模板编号：" prop="templateCode">
              <el-input v-model="queryParams.templateCode" placeholder="请输入模板编号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="模板名称：" prop="templateName">
              <el-input v-model="queryParams.templateName" placeholder="请输入模板名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="模板类型：" prop="templateType">
              <el-select v-model="queryParams.templateType" placeholder="请选择模板类型" clearable>
                <el-option v-for="dict in report_template" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="创建时间：" style="width: 308px">
              <el-date-picker v-model="dateRange" value-format="YYYY-MM-DD" type="daterange" range-separator="-"
                start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item>
              <el-button type="success" icon="Search" @click="handleQuery">搜索</el-button>
              <div class="resetBtn" style="margin-left: 6px">
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <el-row>
        <p class="pinformation">报告模板列表</p>
        <p class="totalStyle">共 {{ total }} 条数据</p>
        <el-row style="padding-bottom: 5px; margin-left: 20px">
          <el-button type="success" link icon="Plus" @click="handleAdd">新增报告模板</el-button>
          <el-button style="color: red !important" link icon="delete" @click="handleDelete">批量删除</el-button>
        </el-row>
      </el-row>

      <el-table class="business" v-loading="loading" :data="tableTable" @selection-change="handleSelectionChange"
        ref="mainTableRef" row-key="templateId" :default-expand-all="true"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="模板编号" align="left" prop="templateCode" width="160" />
        <el-table-column label="模板名称" align="center" prop="templateName" :show-overflow-tooltip="true" />
        <el-table-column label="模板类型" align="center" prop="templateType" width="160">
          <template #default="scope">
            <dict-tag :options="report_template" :value="scope.row.templateType" />
          </template>
        </el-table-column>
        <el-table-column label="版本号" align="center" prop="version" width="160" />
        <el-table-column label="创建人" align="center" prop="createUserName" width="100" />
        <el-table-column label="创建时间" align="center" prop="createTime" width="160" />
        <el-table-column width="430" label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="success" @click="addClick(scope.row)" v-if="!scope.row._tableId">新增</el-button>
            <el-button link type="success" @click="editClick(scope.row)" v-if="!scope.row._tableId">编辑</el-button>
            <el-button link type="success" v-if="!scope.row._tableId" @click="handleCopy(scope.row)">复制</el-button>
            <el-button link type="success" @click="handleDelete(scope.row)" v-if="!scope.row._tableId">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
  </div>
</template>

<script setup name="ReportTemplate">

import {
  listTable,
  delTable,
  tableCopy,
} from "@/api/report/reportTemple";
import { getToken } from "@/utils/auth";

import { nextTick, watch } from "vue";
const route = useRoute();

const { proxy } = getCurrentInstance();
const { report_template } = proxy.useShareDict(
  "report_template"
);
const loading = ref(true);
const ids = ref([]);
const total = ref(0);
const tableTable = ref([]);
const dateRange = ref([]);

const data = reactive({
  form: { tableMain: 0 },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  },
  rules: {
    templateCode: [
      { required: true, message: "模板编号不能为空", trigger: "blur" },
    ],
    templateName: [
      { required: true, message: "模板表名称不能为空", trigger: "blur" },
    ],
    appType: [{ required: true, message: "适用类别不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询模块列表 */
function getList() {
  loading.value = true;
  listTable(proxy.addDateRange(queryParams.value, dateRange.value)).then((response) => {
    tableTable.value = response.rows
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  dateRange.value = []
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => {
    return item.templateId
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _templateIds = row.templateId || ids.value;
  proxy.$modal
    .confirm('是否确认删除此数据？')
    .then(function () {
      return delTable(_templateIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => { });
}

function handleAdd() {
  let testParam = getToken() + 'φpageOffice';
  POBrowser.openWindow("/word", "fullscreen=yes;", testParam);
}

function handleCopy(row) {
  let testParam = getToken() + 'φpageOffice';
  POBrowser.openWindow("/word?copyTemplateId:" + row.templateId, "fullscreen=yes;", testParam);
}

function editClick(row) {
  let testParam = getToken() + 'φpageOffice';
  POBrowser.openWindow("/word?templateId:" + row.templateId, "fullscreen=yes;", testParam);
}


function addClick(row) {
  let testParam = getToken() + 'φpageOffice';
  POBrowser.openWindow("/word?templateId1:" + row.templateId, "fullscreen=yes;", testParam);
}

getList();

watch(
  () => total.value,
  () => {
    if (total.value) {
      heightParams.value = [{ tableName: "main", bottomHeight: 73 }];
    } else {
      heightParams.value = [{ tableName: "main", bottomHeight: 21 }];
    }
    getTableHeight();
  }
);

const heightParams = ref([{ tableName: "main", bottomHeight: 21 }]);

function getTableHeight() {
  proxy.adaptiveHeight(proxy, heightParams.value);
}

onMounted(() => {
  nextTick(() => {
    window.getList = getList;
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
  })
})
onUpdated(() => {
  nextTick(function () {
    getTableHeight();
  });
});
onBeforeUnmount(() => {
  window.removeEventListener("resize", getTableHeight);
});
</script>
